// tbsuggest.js


//class TBSuggest
function TBSuggest(strTBSTextboxID, aDataArray, iKeyFieldIndex, iDropdownSize)
{
	// class private constants 
	var DEFAULT_DATA_ARRAY = [[""]];
	var DEFAULT_KEY_FIELD_INDEX = 0;
	var DEFAULT_DROPDOWN_SIZE = 10;
	
	// class private properties
	this.listItems = new Array();
	
	// reference to controls
	this.textbox = $(strTBSTextboxID);
	this.textbox.container = this;
	this.textbox.setAttribute("autocomplete", "off");
	this.dropdown = $(strTBSTextboxID + "-dropdown");
	this.dropdown.container = this;
	
	// class public properties
	this.id = strTBSTextboxID;
	this.value = this.textbox.value;
	this.selectedIndex = this.dropdown.selectedIndex;
	this.dataArray = aDataArray || DEFAULT_DATA_ARRAY;
	this.keyFieldIndex = iKeyFieldIndex || DEFAULT_KEY_FIELD_INDEX;
	this.dropdownSize = iDropdownSize || DEFAULT_DROPDOWN_SIZE;
	
	
	// set event handlers
	this.textbox.onkeyup = this.textboxKeyupEventHandler;
	this.textbox.onkeydown = this.textboxKeydownEventHandler;
	this.dropdown.onclick = this.dropdownClickEventHandler;
	this.textbox.onblur = this.textboxBlurEventHandler;
}

//handles KeyUp event in textbox
TBSuggest.prototype.textboxKeyupEventHandler = function(event)
{
	var e = event || window.event;
	this.containerInstant = this.container;
	this.containerInstant.value = this.value;

	if (this.value && e.keyCode != "13" && e.keyCode != "38" && e.keyCode != "40")
	{
		this.containerInstant.showDropdown();
		this.containerInstant.dropdown.selectedIndex = 0;
		this.containerInstant.textboxKeyup();
	}
}

// set these methods to invoke other code
TBSuggest.prototype.textboxKeyup = function()
{
	nextBtn.disabled = false;
	rp.style.display = "block";
}
TBSuggest.prototype.textboxEnterKeydown = function()
{
	//add code here
}
TBSuggest.prototype.textboxUpCursorKeydown = function()
{
	//add code here
}
TBSuggest.prototype.textboxDownCursorKeydown = function()
{
	//add code here
}

//handles KeyDown event in textbox
TBSuggest.prototype.textboxKeydownEventHandler = function(event)
{
	var e = event || window.event;
	this.containerInstant = this.container;
	this.containerInstant.value = this.value;
	
	if (this.value && e.keyCode == "13")
	{
		this.containerInstant.fill();
		this.containerInstant.hideDropdown();
		this.containerInstant.textboxEnterKeydown();
		
		return false;
	}
	else if (this.value && e.keyCode == "38")
	{
		if (this.containerInstant.dropdown.selectedIndex != 0)
		{
			this.containerInstant.dropdown.selectedIndex -= 1;
			this.containerInstant.textboxUpCursorKeydown();
			
			return false;
		}
	}
	else if (this.value && e.keyCode == "40")
	{
		if (this.containerInstant.dropdown.selectedIndex != this.containerInstant.dropdown.options.length - 1)
		{
			this.containerInstant.dropdown.selectedIndex += 1;
			this.containerInstant.textboxDownCursorKeydown();
			
			return false;
		}
	}
}

//handles mouse click event in dropdown menu
TBSuggest.prototype.dropdownClickEventHandler = function(event)
{
	this.containerInstant = this.container;

	if (this.value)
	{
		this.containerInstant.fill();
		this.containerInstant.hideDropdown();
	}

}

//handles blur event in textbox
TBSuggest.prototype.textboxBlurEventHandler = function(event)
{
	this.containerInstant = this.container;
	this.containerInstant.hideDropdown();
}

//to fill the value of textbox with dropdown's selection
TBSuggest.prototype.fill = function()
{
	this.textbox.value = this.dropdown.value;
	this.value = this.textbox.value;
	
	//var listItem = getListItem(this.value, 0);//this.keyFieldIndex);
	
	//pn.value = listItem[0];
	//fn.value = listItem[1];
	//ln.value = listItem[2];
}

//to initialize the dropdown menu and to display it
TBSuggest.prototype.showDropdown = function()
{
	this.getDropdown();
	//this.dropdown.style.width = this.textbox.style.width;
	//this.dropdown.style.marginTop = parseInt(this.textbox.style.height) + "px";
	if (this.listItems.length > 0)
	{
		this.dropdown.style.visibility = "visible";
	}
}

//to hide dropdown menu
TBSuggest.prototype.hideDropdown = function()
{
	this.dropdown.style.visibility = "hidden";
}

//TODO: Change this method into a AJAX function, use the value property as query to request new data array
//to put filtered input data into array property
TBSuggest.prototype.bindData = function()
{
	var aAllData = this.dataArray;
	
	this.listItems = new Array();
	
	for (var i = 0; i < aAllData.length; i++)
	{
		if (aAllData[i][this.keyFieldIndex].substring(0, this.value.length).toLowerCase() == this.value.toLowerCase())
		{
			this.listItems.push(aAllData[i]);
		}
	}
}

//to transform data array to options of the select object
TBSuggest.prototype.getDropdown = function()
{
	this.bindData();
	this.dropdown.length = (this.listItems.length < this.dropdownSize) ? this.listItems.length : this.dropdownSize;
	
	for (var i = 0; i < this.listItems.length && i < this.dropdownSize; i++)
	{
		this.dropdown.options[i].text = this.listItems[i][0] + ": " + this.listItems[i][1]+ " " + this.listItems[i][2] + ", " + this.listItems[i][3];
		this.dropdown.options[i].value = this.listItems[i][this.keyFieldIndex];
	}
}

//class TBSuggestGroup
function TBSuggestGroup(astrTBSTextboxID, aDataArray, aiKeyFieldIndex, iDropdownSize)
{
	// class private constants 
	var DEFAULT_DATA_ARRAY = [[""]];

	// reference to controls
	this.textboxes = new Array();
		
	// class public properties
	this.TBSuggests = new Array();
	this.size = astrTBSTextboxID.length;
	this.dataArray = aDataArray || DEFAULT_DATA_ARRAY;

	for (var i = 0; i < this.size; i++)
	{
		this.TBSuggests.push(new TBSuggest(astrTBSTextboxID[i], aDataArray, aiKeyFieldIndex[i], iDropdownSize));
		this.textboxes.push(this.TBSuggests[i].textbox);

		this.TBSuggests[i].fill = this.fill;
		this.TBSuggests[i].container = this;
	}
}

// override TBSuggest.fill()
TBSuggestGroup.prototype.fill = function()
{
	var container = this.container;
	var listItem;
	
	this.textbox.value = this.dropdown.value;
	listItem = getListItem(this.textbox.value, container.dataArray, this.keyFieldIndex);
	
	for (var i = 0; i < container.size; i++)
	{

		if (i != this.keyFieldIndex)
		{
			container.textboxes[i].value = listItem[container.TBSuggests[i].keyFieldIndex];
			container.TBSuggests[i].value = container.textboxes[i].value;
		}
	}
}

//build a fixed length string using a given one; not in use.
function getFixedLengthString(strInput, iLength)
{
	var strTemp = "";
	
	for (var i = 0; i < iLength; i++)
	{
		if (i < strInput.length)
		{
			strTemp += strInput.substring(i, i + 1);
		}
		else
		{
			strTemp += " ";
		}
	}
	
	return strTemp;
}

//search the object in array of which Key field matches the query string
function getListItem(strMatch, aListData, iKeyIndex)
{
	var aAllData = aListData;
	
	for (var i = 0; i < aAllData.length; i++)
	{
		if (aAllData[i][iKeyIndex] == strMatch)
		{
			return aAllData[i];
		}
	}
	
	return ["", "", "", ""]
}